package zhxt.helmet.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import zhxt.helmet.pojo.User;
import zhxt.helmet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/analogUser")
    @ResponseBody
    public ResponseEntity<User> getAnalogUser( ) {
        User user = new User();
        user.setUser(1,"li","1",1,2,"全部",2,"全部",2,"全部",2,"全部");
        return ResponseEntity.ok(user);
    }

    @GetMapping("/current-user")
    @ResponseBody
    public ResponseEntity<User> getCurrentUser(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        return ResponseEntity.ok(user);
    }

    @RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String login(User u, HttpSession session) throws JsonProcessingException {
        // 后台登录验证
        User user = userService.login(u);
        if (user != null && user.getName() != null) {
            // 验证通过且，则将user对象存入会话中
            session.setAttribute("user", user);
            // 以JSON格式向页面发送成功信息
            return "{\"success\":\"true\",\"message\":\"登录成功\"}";
        } else
            return "{\"success\":\"false\",\"message\":\"登录失败：用户id或密码错误\"}";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> userList() {
        // 根据查询条件，获取客户列表
        List<User> userList = userService.allUsers();
        // 创建对象result，保存查询结果数据
        Map<String, Object> result = new HashMap<String, Object>(2);
        result.put("total", userList.size());
        result.put("rows", userList);
        return result;
    }


    // 退出
    @RequestMapping(value = "/logout")
    @ResponseBody
    public String logout(HttpSession session) {
        session.invalidate(); // 销毁整个 Session（所有属性都会被清除）

        return "{\"success\":\"true\",\"message\":\"注销成功\"}";
    }

    // 添加用户
    @PostMapping(value = "/addUser", produces = "text/html;charset=UTF-8",
            consumes = "application/x-www-form-urlencoded")
    @ResponseBody
    public String addUser( User ui) {
        try {
            userService.addUser(ui);
            return "{\"success\":\"true\",\"message\":\"用户添加成功\"}";
        } catch (Exception e) {
            return "{\"success\":\"false\",\"message\":\"用户添加失败\"}";
        }
    }
    // 修改用户
    @RequestMapping(value = "/updateUser", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateUser( User ui) {
        try {
            userService.updateUser(ui);
            return "{\"success\":\"true\",\"message\":\"用户修改成功\"}";
        } catch (Exception e) {
            return "{\"success\":\"false\",\"message\":\"用户修改失败\",\"errorMsg\":\"失败\"}";
        }
    }
    //删除用户
    @RequestMapping(value = "/deleteUser", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteUser(User ui) {
        try {
            userService.deleteUser(ui);
            return "{\"success\":\"true\",\"message\":\"用户修改成功\"}";
        } catch (Exception e) {
            return "{\"success\":\"false\",\"message\":\"用户修改失败\"}";
        }
    }
}
